package neves.android.etmg;


import neves.android.etmg.input.InputManager;
import neves.android.etmg.map.GameMap;
import neves.android.etmg.tilemap.TileGameCore;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;

public class ETMGView extends SurfaceView implements SurfaceHolder.Callback {

	/** Pointer to the text view to display "Paused.." etc. */
	private TextView mStatusText;

	//private InputManager inputAgent;
	
	/** The thread that actually draws the animation */
	//private ETMGThread thread;
	private GameCore thread;

	//private MapRender mapRender;
	private GameMap currentMap;

	//private MapCreater tileMapCreater;

	//private MovableObject playViewer;

	public ETMGView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		
		// register our interest in hearing about changes to our surface
		SurfaceHolder holder = getHolder();
		holder.addCallback(this);

		// create thread only; it's started in surfaceCreated()
		thread = new TileGameCore(holder, context, new Handler() {
			@Override
			public void handleMessage(Message m) {
				Log.w("ETMG", String.valueOf(m.getData().getInt("viz")));
				mStatusText.setVisibility(m.getData().getInt("viz"));
				mStatusText.setText(m.getData().getString("text"));
			}
		});

		setMap(thread.GetMap());
		
		setFocusable(true); // make sure we get key events

	}

	/**
	 * Installs a pointer to the text view used for messages.
	 */
	public void setTextView(TextView textView) {
		mStatusText = textView;
	}

	public void setMap(GameMap newMap){
		currentMap = newMap;

	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		thread.setSurfaceSize(width, height);

		if (width < height) {
			currentMap.GetMapRender().SetViewSize(width, width);
		} else {
			currentMap.GetMapRender().SetViewSize(height, height);
		}

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub

		thread.setRunning(true);
		thread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub

	}

	/**
	 * Standard override to get key-press events.
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent msg) {
		// return false allow next handler to reaction or return true;
		//return inputAgent.onKeyDown(keyCode, msg);
		//return false;
		return thread.doKeyDown(keyCode, msg);
	}

	/**
	 * Standard override for key-up. We actually care about these, so we can
	 * turn off the engine or stop rotating.
	 */
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent msg) {
		//return inputAgent.onKeyUp(keyCode, msg);
		//return false;
		return thread.doKeyUp(keyCode, msg);// thread.doKeyUp(keyCode, msg);
	}

	/**
	 * Standard window-focus override. Notice focus lost so we can pause on
	 * focus lost. e.g. user switches to take a call.
	 */
	@Override
	public void onWindowFocusChanged(boolean hasWindowFocus) {
		if (!hasWindowFocus)
			thread.pause();
	}
	
	/**
	 * Fetches the animation thread corresponding to this View.
	 * 
	 * @return the animation thread
	 */
	public GameCore getThread() {
		return thread;
	}
	
	

	

}
